<?php

/**
 * This is the model class for table "movie_company".
 *
 * The followings are the available columns in table 'movie_company':
 * @property string $movie_id
 * @property string $company_id
 */
class MovieCompany extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return MovieCompany the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'movie_company';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('movie_id, company_id', 'required'),
            array('movie_id, company_id', 'length', 'max' => 10),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'movie_id' => 'Movie',
            'company_id' => 'Company',
        );
    }

    /**
     * Find companies by movie ID
     * 
     * @param int $movieId  Contains ID of movie
     * 
     * @return array
     */
    public function findCompaniesByMovie($movieId) {
        $result = $this->dbConnection->createCommand()
                ->select('c.name')
                ->from('movie_company mc')
                ->join('company c', 'mc.company_id=c.id')
                ->where('mc.movie_id=:movieId', array(':movieId' => $movieId))
                ->queryColumn();
        return $result;
    }

    /**
     * Delete records by movie ID
     * 
     * @param int $movieId  Contains ID of movie
     */
    public function deleteByMovie($movieId) {
        $this->deleteAll('movie_id = :movieId', array(':movieId' => $movieId));
    }

    /**
     * Delete records by company ID
     * 
     * @param int $companyId  Contains ID of company
     */
    public function deleteByCompany($companyId) {
        $this->deleteAll('company_id = :companyId', array(':companyId' => $companyId));
    }

}